package com.ks.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ks.entity.Profession;
import com.ks.entity.Rank;
import com.ks.service.ProfessionService;
import com.ks.service.RankService;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.ks.base.BaseAction;
import com.ks.entity.ExamParenttype;
import com.ks.service.ExamParenttypeService;
import com.ks.util.HtmlUtil;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("/type")
public class TypeMngController extends BaseAction {
    private final static Logger log = Logger.getLogger(TypeMngController.class);

    // Servrice start
    @Autowired(required = false) //自动注入，不需要生成set方法了，required=false表示没有实现类，也不会报错。
    private ExamParenttypeService<ExamParenttype> examParenttypeService;
    // Servrice start
    @Autowired(required = false) //自动注入，不需要生成set方法了，required=false表示没有实现类，也不会报错。
    private ProfessionService<Profession> professionService;
    @Autowired(required = false) //自动注入，不需要生成set方法了，required=false表示没有实现类，也不会报错。
    private RankService<Rank> rankService;

    /**
     * @param entity
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping("/pagedata")
    public ModelAndView pagedata(ExamParenttype entity, HttpServletRequest request) throws Exception {

        Map<String, Object> context = getRootMap();

        return forword("pages/typeMng/type", context);
    }

    @RequestMapping("/dataList")
    @ResponseBody
    public void datalist(HttpServletResponse response) throws Exception {

        List<JSONObject> list = examParenttypeService.queryJson(null);

        HtmlUtil.writerJson(response, list);
    }

    /**
     * 添加
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping("/save")
    @ResponseBody
    public void save(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map<String, Object> context = getRootMap();
        String type = request.getParameter("type");
        String tempid = request.getParameter("tempid");
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String code = request.getParameter("code");
        Object obj = null;
        String msg = "";
        String status = "1";
        Map<String, Object> map = new HashMap<>();
        if (null==type || "".equals(type)){
            ExamParenttype entity = new ExamParenttype();
            entity.setName(name);
            entity.setCode(code);
            map.put("code",code);
            List<ExamParenttype> examParenttypes = examParenttypeService.queryList(map);
            if (examParenttypes.size()==0) {
                examParenttypeService.add(entity);
            } else {
                status = "0";
                msg = "大类代码重复";
            }
            entity.setExtend("type");
            obj = entity;

        } else if ("type".equals(type)){
            Profession entity = new Profession();
            entity.setName(name);
            entity.setCode(code);
            entity.setType_id(tempid);
            map.put("code",code);
            List<Profession> professions = professionService.queryList(map);
            if (professions.size()==0) {
                professionService.add(entity);
            } else {
                status = "0";
                msg = "专业代码重复";
            }
            entity.setExtend("profession");
            obj = entity;
        } else if ("profession".equals(type)) {
            Rank entity = new Rank();
            entity.setName(name);
            entity.setCode(code);
            entity.setPro_id(Integer.parseInt(tempid));
            map.put("code",code);
            List<Rank> ranks = rankService.queryList(map);
            if (ranks.size()==0) {
                rankService.add(entity);
            } else {
                status = "0";
                msg = "专业代码重复";
            }
            entity.setExtend("rank");
            obj = entity;
        }
        context.put("obj",obj);
        context.put("msg",msg);
        context.put("status",status);
        HtmlUtil.writerJson(response, context);
    }
    /**
     * 修改数据
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping("/update")
    @ResponseBody
    public void update(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map<String, Object> context = getRootMap();
        String type = request.getParameter("type");
        String tempid = request.getParameter("tempid");
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String code = request.getParameter("code");
        String msg = "";
        String status = "1";
        Object obj = null;
        Map<String, Object> map = new HashMap<>();
        if ("type".equals(type)){
            ExamParenttype entity = new ExamParenttype();
            entity.setName(name);
            entity.setCode(code);
            entity.setId(Integer.parseInt(id));
            ExamParenttype examParenttype = examParenttypeService.queryById(id);
            if (code.equals(examParenttype.getCode())) {
                examParenttypeService.updateBySelective(entity);
            } else {
                map.put("code",code);
                List<ExamParenttype> examParenttypes = examParenttypeService.queryList(map);
                if (examParenttypes.size()==0) {
                    examParenttypeService.updateBySelective(entity);
                } else {
                    status = "0";
                    msg = "大类代码重复";
                }
            }
            obj = entity;
        } else if ("profession".equals(type)) {
            Profession entity = new Profession();
            entity.setName(name);
            entity.setCode(code);
            entity.setId(Integer.parseInt(id));
            Profession profession = professionService.queryById(id);
            if (code.equals(profession.getCode())) {
                professionService.updateBySelective(entity);
            } else {
                map.put("code",code);
                List<Profession> professions = professionService.queryList(map);
                if (professions.size()==0) {
                    professionService.updateBySelective(entity);
                } else {
                    status = "0";
                    msg = "专业代码重复";
                }
            }
            obj = entity;
        } else {
            Rank entity = new Rank();
            entity.setName(name);
            entity.setCode(code);
            entity.setId(Integer.parseInt(id));
            Rank rank = rankService.queryById(id);
            if (code.equals(rank.getCode())) {
                rankService.updateBySelective(entity);
            } else {
                map.put("code",code);
                List<Rank> ranks = rankService.queryList(map);
                if (ranks.size()==0) {
                    rankService.updateBySelective(entity);
                } else {
                    status = "0";
                    msg = "专业代码重复";
                }
            }
            obj = entity;
        }
        context.put("obj",obj);
        context.put("msg",msg);
        context.put("status",status);
        HtmlUtil.writerJson(response, context);
    }
    @RequestMapping("/getId")
    public void getId(String id, HttpServletResponse response) throws Exception {

        HtmlUtil.writerJson(response, "");
    }


    @RequestMapping("/del")
    public void delete(HttpServletRequest request, HttpServletResponse response) throws Exception {

        String type = request.getParameter("type");
        String tempid = request.getParameter("tempid");
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String code = request.getParameter("code");
        Object obj = null;
        if ("type".equals(type)) {
            examParenttypeService.del(id);
        } else if ("profession".equals(type)) {
            professionService.del(id);
        } else if ("rank".equals(type)) {
            rankService.del(id);
        }
        sendSuccessMessage(response, "删除成功");
    }

}
